// CurveDrawer.cpp: implementation of the CurveDrawer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwclientcore.h"
#include "CurveDrawer.h"

#include "CommonLib\TimeUtility.h"
#include "CommonLib\RecordManager.h"

#include "MonitoringUnitLib\BaseMonitoringUnit.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CurveDrawer::CurveDrawer()
{

}

CurveDrawer::~CurveDrawer()
{

}

void CurveDrawer::DrawCoordinateGrid(CDC& dc, RealtimeCurveInfo * pRealtimeCurveInfo)
{
  CPen * pOldPen = (CPen *)dc.SelectStockObject(BLACK_PEN);

  CRect rect = pRealtimeCurveInfo->m_rect;
  rect.DeflateRect(40, 30, 0, 0);
  DrawTimeAxis(dc, rect, pRealtimeCurveInfo->m_timeBegin, pRealtimeCurveInfo->m_timeEnd);
  DrawDataAxis(dc, rect, pRealtimeCurveInfo->m_pMonitoringUnit);
  CBrush * pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(BLACK_BRUSH));
  dc.FrameRect(&rect, pBrush);

  dc.SelectObject(pOldPen);
}

void CurveDrawer::DrawTimeAxis(CDC& dc, const CRect& rect, const CTime& timeBegin, const CTime& timeEnd)
{
	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm);

	dc.MoveTo(rect.left, rect.top-5);
	dc.LineTo(rect.left, rect.top);

	CString str = timeBegin.Format("%H:%M:%S");
	CSize size = dc.GetTextExtent(str);
  int x = rect.left;
  int y = rect.top-(tm.tmHeight+tm.tmExternalLeading+5);
	dc.TextOut(x, y, str);
	int xr = x+size.cx;

	dc.MoveTo(rect.right-1, rect.top-5);
	dc.LineTo(rect.right-1, rect.top);

  str = timeEnd.Format("%H:%M:%S");
	size = dc.GetTextExtent(str);

	x = rect.right-size.cx;
	y = rect.top-(tm.tmHeight+tm.tmExternalLeading+5);
	dc.TextOut(x, y, str);
	int xl = x;

  CTime time(timeBegin.GetYear(), timeBegin.GetMonth(), timeBegin.GetDay(), timeBegin.GetHour(), 0, 0);
  TimeUtility::IncHour(time);
  while (time < timeEnd)
  {
    float fPercentage = TimeUtility::TimePercentage(timeBegin, timeEnd, time);
    str = time.Format("%H");
    size = dc.GetTextExtent(str);

    x = ROUND(rect.left+rect.Width()*fPercentage-size.cx/2);
    y = rect.top-(tm.tmHeight+tm.tmExternalLeading+5);
    if (x+size.cx < xl && x > xr) 
    {
      dc.TextOut(x, y, str);
      dc.MoveTo(ROUND(rect.left+rect.Width()*fPercentage), rect.top-5);
      dc.LineTo(ROUND(rect.left+rect.Width()*fPercentage), rect.top);
      xr = x+size.cx;
    }
    CPen * pPen = new CPen(PS_SOLID, 1, RGB(192, 192, 192));
    CPen * pOldPen = dc.SelectObject(pPen);
    dc.MoveTo(ROUND(rect.left+rect.Width()*fPercentage), rect.top);
    dc.LineTo(ROUND(rect.left+rect.Width()*fPercentage), rect.bottom);
    dc.SelectObject(pOldPen);
    delete pPen;
    TimeUtility::IncHour(time);
  }
}

void CurveDrawer::DrawDataAxis(CDC& dc, const CRect& rect, BaseMonitoringUnit * pMonitoringUnit)
{
  pMonitoringUnit->DrawRTCurveDataAxis(dc, rect);
}

void CurveDrawer::DrawRealtimeCurve(CDC& dc, RealtimeCurveInfo * pRealtimeCurveInfo)
{
  if (pRealtimeCurveInfo->m_pRealtimeRecordMgr->GetRecords().size() == 0) return;

  CRect rect = pRealtimeCurveInfo->m_rect;
  rect.DeflateRect(50, 30, 0, 0);
	CRgn * pRgn = new CRgn;
	pRgn->CreateRectRgn(rect.left, rect.top, rect.right, rect.bottom);
	dc.SelectClipRgn(pRgn, RGN_COPY);

	CPen * pPen = new CPen(PS_SOLID, pRealtimeCurveInfo->m_nPenWidth, pRealtimeCurveInfo->m_crColor);
	CPen * pOldPen = dc.SelectObject(pPen);

  pRealtimeCurveInfo->m_pMonitoringUnit->DrawRTCurve(dc, rect, pRealtimeCurveInfo->m_pRealtimeRecordMgr, pRealtimeCurveInfo->m_timeBegin, pRealtimeCurveInfo->m_timeEnd);

  dc.SelectObject(pOldPen);
  delete pPen;
  delete pRgn;
}
